package commands;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CopyObjectRequest;
import com.amazonaws.services.s3.model.CopyObjectResult;
import jesss.commands.JesssCommand;
import jesss.commands.JesssCommandFactoryImpl;
import jesss.commands.CommandResult;
import jesss.commands.ResultStatus;
import jesss.options.JesssOptions;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

/**
 * User: Lee
 * Date: 8/26/13
 * Time: 10:38 PM
 */
public class CopyObjectCommandTests extends CommandTestsBase {
    private AmazonS3 mockS3 = mock(AmazonS3.class);
    private JesssOptions mockJesssOptions = mock(JesssOptions.class);

    @Before
    public void setUp() throws Exception {
        stub(mockJesssOptions.getAmazonS3()).toReturn(mockS3);
        stub(mockS3.copyObject(any(CopyObjectRequest.class))).toReturn(makeCopyObjectResult());
        stub(mockJesssOptions.getCommandName()).toReturn("co");
        stub(mockJesssOptions.getShouldDisplayVerbose()).toReturn(true);
    }

    @Test
    public void CanCopyObject() throws Exception {
        String[] args = {"-sbn", "testBucket1", "-sk", "skey", "-tbn", "targetBucket", "-tk", "target key"};
        JesssCommand copyObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        CommandResult<CopyObjectResult> commandResult = copyObjectCommand.executeCommand();

        Assert.assertEquals("Expected copy to be successful.", ResultStatus.Success, commandResult.getResultStatus());
        if (commandResult.getResultStatus() == ResultStatus.Success) {
            CopyObjectResult copyObjectResult = commandResult.getResultValue();
            Assert.assertNotNull("Returned CopyObjectResult was null, but should not be null.", copyObjectResult);
        }
        verify(mockS3, times(1)).copyObject(any(CopyObjectRequest.class));
    }

    @Test(expected=org.apache.commons.cli.MissingArgumentException.class)
    public void ThrowForMissingSourceBucketParam() throws Exception {
        String[] args = {"-sk", "testPrefix", "-tbn", "asdf", "-tk", "asfd"};
        JesssCommand copyObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        copyObjectCommand.executeCommand();
    }

    @Test(expected=org.apache.commons.cli.MissingArgumentException.class)
    public void ThrowForMissingTargetBucketParam() throws Exception {
        String[] args = {"-sk", "testPrefix", "-sbn", "asdf", "-tk", "asfd"};
        JesssCommand copyObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        copyObjectCommand.executeCommand();
    }

    @Test(expected=org.apache.commons.cli.MissingArgumentException.class)
    public void ThrowForMissingTargetKeyParam() throws Exception {
        String[] args = {"-sk", "testPrefix", "-sbn", "asdf", "-tbn", "asfd"};
        JesssCommand copyObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        copyObjectCommand.executeCommand();
    }

    @Test(expected=org.apache.commons.cli.MissingArgumentException.class)
    public void ThrowForMissingSourceKeyParam() throws Exception {
        String[] args = {"-sbn", "wrew", "-sbn", "asdf", "-tk", "asfd"};
        JesssCommand copyObjectCommand = JesssCommandFactoryImpl.getInstance().createCommand(mockS3, mockJesssOptions.getCommandName(), mockJesssOptions.getShouldDisplayVerbose(), args);

        copyObjectCommand.executeCommand();
    }
}
